package com.example.memo1.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.memo1.R;
import com.example.memo1.dao.AdminDao;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;

public class Add extends AppCompatActivity {
    private ImageView fan;

    String uuid=null;
    String account=null;
    EditText res=null;
    EditText title=null;
    TextView textView=null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        //加载菜单条
        Toolbar toolbar = findViewById(R.id.toolbar_add);
        setSupportActionBar(toolbar);

        ImageView fan = toolbar.findViewById(R.id.fan);
        fan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 =new Intent(Add.this,MainActivity.class);
                startActivity(intent1);

            }
        });

        //获取账号
        Intent intent=getIntent();
         account=intent.getStringExtra("account");



         textView = findViewById(R.id.add_time);

        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 a HH:mm");
        String curTime = dateFormat.format(calendar.getTime());

        curTime = curTime+ "      |     ";

        curTime = curTime + 0 + "字";
        textView.setText(curTime);


         res = findViewById(R.id.con);
         title = findViewById(R.id.title);
        res.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
//获取当前月份时间
                Calendar calendar = Calendar.getInstance();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 a HH:mm");
                String curTime = dateFormat.format(calendar.getTime());

                curTime = curTime + "      |     ";

                String text = String.valueOf(res.getText());
                String zi = String.valueOf(text.length());
                curTime = curTime + zi + "字";
                textView.setText(curTime);
                //每次输入一个字符，自动保存
                String titles=title.getText().toString().trim();
                String rest=res.getText().toString().trim();
                int a=new AdminDao().saveRecord(uuid,titles,rest,account,curTime);
            }
        });

         uuid =UUID.randomUUID().toString();//随机uuid


    }

    //菜单

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.add_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == R.id.add_menu_save) {//保存
            String curTime=textView.getText().toString().trim();
            String titles=title.getText().toString().trim();
            String rest=res.getText().toString().trim();
            int a=new AdminDao().saveRecord(uuid,titles,rest,account,curTime);
            if(a==1){
                Toast.makeText(Add.this, "保存成功！", Toast.LENGTH_SHORT).show();
            }else {
            Toast.makeText(Add.this, "保存失败！", Toast.LENGTH_SHORT).show();
        } }else if (item.getItemId() == R.id.add_menu_del) {//删除

            new AdminDao().delRecord(uuid,account);
            Intent intent=new Intent(Add.this,MainActivity.class);
            startActivity(intent);
        }
      return super.onOptionsItemSelected(item);
    }




}

